/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.information.rest;

import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.information.domain.SlideItem;
import me.zhengjie.modules.information.domain.resp.SlideItemListResp;
import me.zhengjie.modules.information.service.SlideItemService;
import me.zhengjie.modules.information.domain.vo.SlideItemQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author yiwei.he
* @date 2024-05-08
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "轮播图子项管理")
@RequestMapping("/api/slideItem")
@Slf4j
public class SlideItemController {

    private final SlideItemService slideItemService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check()")
    public void exportSlideItem(HttpServletResponse response, SlideItemQueryCriteria criteria) throws IOException {
        slideItemService.download(slideItemService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询轮播图子项")
    @ApiOperation("查询轮播图子项")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<PageResult<SlideItem>> querySlideItem(SlideItemQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(slideItemService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增轮播图子项")
    @ApiOperation("新增轮播图子项")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> createSlideItem(@Validated @RequestBody SlideItem resources){
        slideItemService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改轮播图子项")
    @ApiOperation("修改轮播图子项")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> updateSlideItem(@Validated @RequestBody SlideItem resources){
        slideItemService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除轮播图子项")
    @ApiOperation("删除轮播图子项")
//    @PreAuthorize("@el.check()")
    public ResponseEntity<Object> deleteSlideItem(@RequestBody List<Long> ids) {
        slideItemService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @PostMapping("/list")
    @Log("小程序获取轮播图")
    @ApiOperation("查询轮播图子项")
    @AnonymousAccess
    public ResponseEntity<SlideItemListResp> getSlideList(@RequestBody SlideItemQueryCriteria criteria) throws BizException {
        return new ResponseEntity<>(slideItemService.getSlideList(criteria),HttpStatus.OK);
    }

}
